package com.yenlien.traceyun.plant.utils;

import java.text.DecimalFormat;

/**
 * @Date 2022/7/29
 * @Author jiaLong
 * @Descript
 */
public class MathUtils {

    public static DecimalFormat df = new DecimalFormat("###.##");

    public static DecimalFormat df6 = new DecimalFormat("###.######");

    public static DecimalFormat df4 = new DecimalFormat("###.####");

    public static DecimalFormat df1 = new DecimalFormat("###.#");

    /**
     * 两个浮点数 a b 的百分比,保留两位小数并四舍五入,末尾是0则去除
     * @param a
     * @param b
     * @return
     */
    public static String percentage(Float a, Float b){
        if(a == null || a == 0){
            return "0%";
        }
        if(b == null || b == 0){
            return "100%";
        }
        return df.format((a / b) * 100) + "%";
    }

    public static String percentage(Double a, Double b){
        if(a == null || a == 0) return "0%";
        if(b == null || b == 0) return "100%";
        return df.format((a / b) * 100) + "%";
    }



    public static String percentage(String a, String b){
        Float aFloat = Float.parseFloat(a);
        Float bFloat = Float.parseFloat(b);
        return percentage(aFloat, bFloat);
    }
    public static String percentage(Integer a, Integer b){
        if(a == null || a == 0){
            return "0%";
        }
        if(b == null || b == 0){
            return "0%";
        }
        Float aFloat = Float.valueOf(a);
        Float bFloat = Float.valueOf(b);
        return percentage(aFloat, bFloat);
    }

    public static String percentage(Long a, Long b){
        return percentage(Integer.valueOf(String.valueOf(a)), Integer.valueOf(String.valueOf(b)));
    }

    public static String div(Float a, Float b){
        return df.format((a / b));
    }

    public static String div(Integer a, Integer b){
        return df.format((Float.valueOf(a) / Float.valueOf(b) * 100));
    }

    /**
     * 格式化浮点数保留两位小数
     * @param a
     * @return
     */
    public static String format(Float a){
        return df.format(a);
    }


    public static String format(Double a){
        return df.format(a);
    }

    public static String formatSix(Double a){
        return df6.format(a);
    }

    public static String formatOne(Double a){
        return df1.format(a);
    }

    public static String formatFour(Double a){
        return df6.format(a);
    }




}
